package com.xbongbong.workflow.service.operator.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.help.AddressHelp;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.constant.BusinessConstant;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.paas.pojo.workflow.WorkflowDataPojo;
import com.xbongbong.workflow.pojo.WorkflowOperationsPojo;
import com.xbongbong.workflow.service.operator.WorkflowOperatorFieldHandler;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2020/3/4 21:11
 * @since v1.0
 */
@Service("workflowOperatorAddressHandlerImpl")
public class WorkflowOperatorAddressHandlerImpl implements WorkflowOperatorFieldHandler {

    @Override
    public List<Integer> type() {
        return Arrays.asList(FieldTypeEnum.ADDRESS.getType());
    }

    @Override
    public Object handlerThisFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity,
                                       WorkflowDataPojo workflowDataPojo, WorkflowOperationsPojo workflowOperationsPojo) throws XbbException {
        // 无需特殊处理
        return data.get(fieldAttrEntity.getAttr());
    }

    @Override
    public Object handlerTargetFormField(PaasFormDataEntity paasFormDataEntity, JSONObject data, FieldAttrEntity fieldAttrEntity, FieldAttrEntity targetField,
                                         WorkflowDataPojo workflowDataPojo, WorkflowOperationsPojo workflowOperationsPojo, WorkflowNodeEntity workflowNodeEntity) throws XbbException {
        JSONObject address = data.getJSONObject(fieldAttrEntity.getAttr());
        setAddress(targetField, address);
        return address;
    }

    @Override
    public Object handlerFixedValue(FieldAttrEntity targetField, WorkflowDataPojo workflowDataPojo, WorkflowOperationsPojo workflowOperationsPojo, WorkflowNodeEntity workflowNodeEntity) throws XbbException {
        JSONObject address = JSON.parseObject(JSON.toJSONString(workflowOperationsPojo.getValue()));
        setAddress(targetField, address);
        return address;
    }

    private void setAddress(FieldAttrEntity targetField, JSONObject address) {
        Integer showDetailAddress = targetField.getShowDetailAddress();
        if (Objects.equals(0, showDetailAddress)) {
            // 当目标表单的地址无详细地址，而当前表单地址字段有详细地址，此时将当前表单中地址字段中的省、市、区赋值给目标表单中的地址字段，丢弃详细地址。
            address.put(BusinessConstant.ADDRESS, "");
        }
    }

    @Override
    public Object formulaHandlerThisField(FieldAttrEntity fieldAttrEntity, PaasFormDataEntity formDataEntity, JSONObject data,
                                          Long formId, Integer businessType, String corpid)  throws XbbException {
        Object address = data.get(fieldAttrEntity.getAttr());
        if (Objects.isNull(address)){
            JSONObject addressJson = new JSONObject();
            addressJson.put(BusinessConstant.PROVINCE, "");
            addressJson.put(BusinessConstant.CITY, "");
            addressJson.put(BusinessConstant.DISTRICT, "");
            addressJson.put(BusinessConstant.ADDRESS, "");
            address = addressJson;
        }
        return JSON.toJSONString(address);
    }

    @Override
    public Object formulaHandlerTargetField(Object value, WorkflowOperationsPojo workflowOperationsPojo, FieldAttrEntity targetField, WorkflowNodeEntity workflowNodeEntity, String corpid) throws XbbException {
        if (value instanceof String && !value.toString().contains(BusinessConstant.PROVINCE)) {
            // 字符串表示是固定值，需要对其进行解析
            // toString()用于去掉手动加的引号
            JSONObject address = AddressHelp.getAddress(value.toString(), targetField);
            return address;
        } else {
            JSONObject address = JSON.parseObject(JSON.toJSONString(value));
            if (Objects.equals(0, targetField.getShowDetailAddress())) {
                address.put(BusinessConstant.ADDRESS, "");
            }
            return address;
        }
    }


}
